package com.dxb.hystrix.comsumer.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;

/**
 * @author DLS
 */
public class UserTimeoutCommand extends HystrixCommand<String> {

    public UserTimeoutCommand() {
        super(Setter
                // 分组名称用于统计
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey("userTimeoutGroup"))
                // 用于监控、熔断、度量发布、缓存的Key值
                .andCommandKey(HystrixCommandKey.Factory.asKey("userTimeoutCommandKey"))
                // 线程池命名，默认是HystrixCommandGroupKey的名称
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("userThreadPool"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        ////隔离方式：线程池和信号量。默认使用线程池
                        //.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
                        ////信号量隔离的模式下，最大的请求数。和线程池大小的意义一样
                        //.withExecutionIsolationSemaphoreMaxConcurrentRequests(2)
                        //超时时间500毫秒
                        .withExecutionTimeoutInMilliseconds(500)
                        //熔断时间（熔断开启后，各5秒后进入半开启状态，试探是否恢复正常）
                        .withCircuitBreakerSleepWindowInMilliseconds(5000)
                )
                // 设置线程池参数
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        // 线程池大小
                        .withCoreSize(1)));
    }

    @Override
    protected String run() throws Exception {
        System.out.println("UserTimeoutCommand run");
        Thread.sleep(666L);
        return "UserTimeoutCommand";
    }

    @Override
    protected String getFallback() {
        System.out.println("UserTimeoutCommand.getFallback()");
        return "UserTimeoutCommand.getFallback()";
    }
}
